<?php

namespace App\controllers;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use App\models\DiskForm;

/**
 *
 */
class DiskController extends \App\base\Controller
{
  private $token;
  private $msg;

  public function init(ServerRequestInterface $request,ResponseInterface $response)
  {
    $this->token = $request->getQueryParams()['token'];
    $model = new DiskForm($this->token,$this->ci);
    $model->init();
    $this->msg = $model->getMessage();
    return $response->withJson($this->msg['data'],$this->msg['code']);
  }

  public function actions(ServerRequestInterface $request,ResponseInterface $response)
  {
    $this->token = $request->getQueryParams()['token'];
    $action = $request->getQueryParams()['action'];
    $request_data = $request->getBody();
    $request_data = json_decode($request_data);
    $model = new DiskForm($this->token,$this->ci);
    switch ($action) {
      case 'list':
        $model->getNode($request_data->parent);
        break;
      case 'mkdir':
        $model->mkdir($request_data->parent,$request_data->dir_name);
        break;
      case 'move':
        $model->move($request_data->parent,$request_data->movelist,$request_data->to);
        break;
      case 'delete':
        $model->delete($request_data->parent,$request_data->deletelist);
        break;
      case 'upload':
        $model->upload($request->getUploadedFiles()['file'],$request->getQueryParams()['dir']);
        break;
      case 'download':
        $model->download($request_data->file);
        break;
      default:
        return $response->withJson([
          'code' => 403,
          'msg' => '这种操作看不懂！'
        ],403);
    }
    $this->msg = $model->getMessage();
    return $response->withJson($this->msg['data'],$this->msg['code']);
  }

  public function upload(ServerRequestInterface $request,ResponseInterface $response)
  {
    $uploadFile = $request->getUploadedFiles()['file'];
    // $uploadFile->moveTo("/tmp/".$uploadFile->getClientFilename());
    return $response->withJson($uploadFile,200);
  }
}
